package simpleftp.server;

import java.net.ServerSocket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Simple serveur TCP creant des thread pour gerer chaque connexion client.
 * 
 * @author lom & gramage & masson
 */
public class FTPServerMain {

	/**
	 * Port d'ecoute.
	 */
	public static final int PORT = 7802;
	public static final double blockingCoefficient = 0.9;
	public static int numberOfCores;
	public static int poolSize;
	public static ExecutorService executorPool;
	/**
	 * Socket serveur d'accueil.
	 */
	private static ServerSocket socket;

	public static void main(String argv[]) throws Exception {
		// TO BE COMPLETED - MAKE IT MULTITHREAD - 4  LINES - OK
		
		numberOfCores = Runtime.getRuntime().availableProcessors();
		poolSize = (int) (numberOfCores / (1 - blockingCoefficient));
		executorPool = Executors.newFixedThreadPool(poolSize);
	
		
		try {
			socket = new ServerSocket(PORT);
			System.out.println("Server up and running.");
			FTPServer.setDebug(true);
			while (true) {
				FTPServer server = new FTPServer(socket.accept());
				// TO BE COMPLETED - 1 LINE - OK
				executorPool.submit(server);
			}
		} finally {
			if (socket != null) {
				socket.close();
			}
		}
	}
}
